﻿using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;

namespace HopNguyenCore.Core
{
    public class Admins
    {
        public const String CURRENT_ADMIN_SESSION_KEY = null;
        readonly HopNguyenEntities db = new HopNguyenEntities();
        private bool _curr;

        public static Admin Current
        {
            get { return HttpContext.Current.Session[CURRENT_ADMIN_SESSION_KEY] as Admin; }
            set { HttpContext.Current.Session[CURRENT_ADMIN_SESSION_KEY] = value; }
        }

        //check login
        public bool Login(string user, string pass)
        {
            try
            {
                Current = db.Admins.FirstOrDefault(u => u.Username.ToLower() == user.ToLower() && u.Password == pass);
                if (Current != null)
                {
                    var admin = db.Admins.Find(Current.Id);
                    admin.LastLogin = DateTime.Now;
                    db.Entry(admin).State = EntityState.Modified;
                    db.SaveChanges();
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }

        #region load menu by admin

        //load group menu
        public string LoadGroupMenu()
        {
            try
            {
                var builder = "";
                var i = 0;
                var b1 = "";
                builder += "<li><a href=\"/cpanel\"><i class=icon-white icon-list></i> Trang chủ</a> ";
                foreach (var item in db.TypePages.Where(a => a.AdminId == Current.Id))
                {
                    i++;
                    var childMenu = LoadChildMenuByGroupMenu(item.Id);
                    if (!string.IsNullOrEmpty(childMenu))
                    {
                        builder += "<li " + i + "><a href=\"#\"><i class=icon-white icon-list></i>" + item.Name + "<span class=collapse></span></a> ";
                        builder += "<ul class=subMenu>" + childMenu + "</ul></li>";
                    }
                    if (_curr)
                    {
                        builder = builder.Replace("" + i, "class=active dropActive");
                        _curr = false;
                    }
                    else
                    {
                        builder = builder.Replace("" + i, "");
                    }
                }
                builder += "<li><a href=\"/cpanel/logs\"><i class=icon-white icon-list></i> System logs</a> ";
                return "<ul class=navigation>" + builder + "</ul>";
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }

        //load child menu by group menu
        private string LoadChildMenuByGroupMenu(int typeId)
        {
            try
            {
                var builder = new StringBuilder();
                var segments = HttpContext.Current.Request.Url.Segments;
                var lstPage = db.Pages.Where(a => a.TypeId == typeId);
                foreach (var page in lstPage.Where(page => page.TypeId == typeId))
                {
                    if (segments[1] != null && segments[1].ToLower() == page.Link.ToLower())
                    {
                        _curr = true;
                        builder.Append("<li class=\"active dropActive\"><a href=\"/cpanel/" + page.Link + (page.Parameter != null ? "?" + page.Parameter : "") + "\"><span>+</span> " + page.Name + "</a></li>");
                    }
                    else
                    {
                        builder.Append("<li><a href=\"/cpanel/" + page.Link + (page.Parameter != null ? "?" + page.Parameter : "") + "\"><span>+</span> " + page.Name + "</a></li>");
                    }
                }
                return builder.ToString();
            }
            catch (Exception ex)
            {
                SystemLogs.WriteLog(ex);
                throw;
            }
        }

        #endregion

    }
}
